<?php 
require '../model/Db.php';
$Db = Db::getInstance();
$user_data = $Db->islogin();
	//文件上传处理
	$path_arr = [];
	if($_FILES){
		foreach ($_FILES['file']['name'] as $k => $v) {
			if($_FILES['file']['error'][$k]>0){
				continue;
			}elseif($_FILES["file"]["size"][$k]>1024*1024*5){
				continue;
			}else{
				$filepath = '/www/wwwroot/syxingchuang.com/nsbbs/public/uploads/file/';
				$timestmp = time();
				$db_filename = $timestmp.'_'.$v;
				array_push($path_arr, $db_filename);
				$v = iconv("utf-8","gb2312",$v);
				$filename = $timestmp.'_'.$v;
				move_uploaded_file($_FILES['file']['tmp_name'][$k],$filepath.$filename);
			}
		}
	}
	$_POST = $Db->escape($_POST);
	$paths = '';
	foreach ($path_arr as $value) {
		$paths .= '"'.$value.'",';
	}
	$paths = trim($paths,',');

	$flag = $Db->save([
		table => 'mail',
		map => [
			title => $_POST['title'],
			body => $_POST['body'],
			file => '{"path":['.$paths.']}',
			from_id => $user_data['id'],
			to_id => $_POST['to']
		]
	]);
	
    $Db->close();
	$msg = $flag ? '私信成功':'网络不稳定';
	echo(json_encode([msg => $msg]));

?>